package com.lms.thread.threadgroup;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Author: 李孟帅
 * @CreateTime: 2020-06-20 09:21
 * @Description:
 */
public class ThreadPoolDemo {
    public static void main(String[] args) throws InterruptedException {

        ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 5, 2, TimeUnit.SECONDS, new ArrayBlockingQueue<>(1), new ThreadFactory() {
            private int count;

            @Override
            public Thread newThread(Runnable r) {
                return new Thread(Thread.currentThread().getThreadGroup(), r, "MyThreadGroup-" + (++count));
            }
        });

        executor.execute(() -> {
            try {
                System.out.println("任务1: .....");
                Thread.sleep(3000);
                System.out.println("任务1:" + Thread.currentThread().getName());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        executor.execute(() -> {
            try {
                System.out.println("任务2: .....");
                Thread.sleep(3000);
                System.out.println("任务2:" + Thread.currentThread().getName());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        executor.execute(() -> {
            try {
                System.out.println("任务3: .....");
                Thread.sleep(3000);
                System.out.println("任务3:" + Thread.currentThread().getName());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        executor.execute(() -> {
            try {
                System.out.println("任务4: .....");
                Thread.sleep(1000);
                System.out.println("任务4:" + Thread.currentThread().getName());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
    }
}
